require(["serverNotify"], function(serverNotify) {
	var id;
	var card_factory;
	var card_list;
	Path.on("/__basename__.html", function(_current_location) {
		var queryString = _current_location.query;
		id = queryString.get("id");
		card_factory;
		// 获取会员卡信息
		coAjax.get(appConfig.card_factory.by_id + id, {
			bus_id: busInfo._id
		}, function(result) {
			if (!result.result) {
				myConfirm("卡信息无效或过期！");
				Path.jump("/main.html");
				return;
			};
			App.set("$Cache.membership.card", card_factory = result.result);
		});

		//获取用户已经拥有的会员卡
		App.set("$Cache.membership.own_current_card", false);
		eventManager.after("getLoginer", function() {
			coAjax.get(appConfig.user.card_list, function(result) {
				App.set("$Cache.membership.card_list", card_list = result.result);

				jSouper.forEach(card_list, function(card) {
					if (card.card_factory_id === id) {
						App.set("$Cache.membership.own_current_card", true);
						return true;
					}
				});
			});
		});

	});

	function _check_loginer() {
		if (!App.get("loginer")) {
			myConfirm("请先登陆或注册成为我们的会员，后来领取会员卡哦！！！(●'◡'●)ﾉ♥ ", function() {
				Path.jump("/sign_in.html?cb_url=" + App.get("$Cache.top.cb_url"));
			});
			return;
		}
	}
	// 用户添加会员卡
	App.set("$Event.membership.add_card_to_user", function() {
		_check_loginer();
		coAjax.post(appConfig.user.card_add_from_factory + id, function(result) {
			if (result.result) {
				App.concat("loginer.card_list", result.result);
				myConfirm("领卡成功，现在就去购物吧！");
				Path.jump("/main.html");
			};
		});
	});
	//用户购买会员卡
	App.set("$Event.membership.go_buy_card", function(e, vm) {
		_check_loginer();
		var pay_method = this.getAttribute("value");
		App.set("$Cache.membership.pay_method", pay_method);
		if (pay_method !== "face_pay") {
			var payWindow = window.open();
			payWindow.document.write("<h2>跳转中，请稍等……</h2>");
		}
		//进行下单
		coAjax.post(appConfig.user.goods_make_ach, {
			goods_id: card_factory.info.buy_goods_id,
			num: 1,
			pay_method: pay_method
		}, function(result) {
			var ach = result.result;
			var ach_id = ach._id;
			//直接确认订单
			coAjax.put(appConfig.user.ach_lock + ach_id, function(result) {
				var ach = result.result;
				App.set("$Cache.membership.ach", ach);
				if (pay_method == "face_pay") { //如果是使用在线支付，会有弹出窗口
					alert("success", "下单完成，请当面与商家完成订单交易");
				} else {
					//生成表单
					coAjax.post(appConfig.alipay.create_direct_pay_by_user_form_ach_id + ach_id, {
						WIDsubject: "购买会员卡【" + card_factory.info.name + "】",
						type: "url"
					}, function(result) {
						var toPayUrl = result.toString;
						payWindow.location.href = toPayUrl;
					});
				}

				/*
				 * Sock连接
				 */
				var user_notify = serverNotify("user");
				user_notify.on("pay_success", function(new_ach_info) {
					Path.emit("/__basename__.html", Path._current_location);
				});
				user_notify.on("ach.state.changed", function(ach_info) {
					if (ach._id == ach_info.ach_id) {
						App.set("$Cache.membership.ach.info.state", ach_info.state);
					}
				});
				user_notify.on("ach.pay_end.call_back_url.success", function(ach_info) {
					Path.emit("/__basename__.html", Path._current_location);
				});
			});
		});
	});
});